package io;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
import java.util.Date;
import java.util.Scanner;

public class Client {

	public static void main(String[] args) throws IOException {
		 // 获取通道
        SocketChannel socketChannel = SocketChannel.open(new InetSocketAddress("127.0.0.1", 8888));

        // 分配1024字节大小的缓冲区
        ByteBuffer buffer = ByteBuffer.allocate(1024);

        Scanner scan = new Scanner(System.in);

        // 输入
        while (scan.hasNext()) {

            String str = scan.next();

            buffer.put((new Date().toString() + "\n" + str).getBytes());

            // 切换回读模式，实质是令limit=position；position=0。
            buffer.flip();
            // 写入通道
            socketChannel.write(buffer);
            // 清空缓冲区
            buffer.clear();
        }
	}
}
